package com.cloudkinto.enumType.storage;

import com.baomidou.mybatisplus.core.enums.IEnum;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 库区类型
 *
 * @author long
 */

public enum StorageAreaEnum implements IEnum<Integer> {

    /**
     * 库区类型
     */
    one(1, "拣货区", false, "正品区"),
    two(2, "存储区", false, "正品区"),
    three(3, "残次品区", true, "次品区"),
    four(4, "暂存区", false, "正品区"),
    five(5, "其他区", false, "正品区");

    private final Integer type;
    private final String typeName;

    /**
     * true次品区 false正品区
     */
    private final Boolean mainType;

    private final String mainTypeName;

    StorageAreaEnum(Integer type, String typeName, Boolean mainType, String mainTypeName) {
        this.type = type;
        this.typeName = typeName;
        this.mainType = mainType;
        this.mainTypeName = mainTypeName;
    }

    public static List<Map<String, Object>> getMapList() {
        ArrayList<Map<String, Object>> res = new ArrayList<>();
        Map<String, List<StorageAreaEnum>> mainTypeGroup = Arrays.stream(values()).collect(Collectors.groupingBy(StorageAreaEnum::getMainTypeName));
        for (Map.Entry<String, List<StorageAreaEnum>> entry : mainTypeGroup.entrySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("label", entry.getKey());
            ArrayList<Map<String, Object>> options = new ArrayList<>();
            entry.getValue().forEach(e -> {
                HashMap<String, Object> option = new HashMap<>();
                option.put("label", e.typeName);
                option.put("type", e.type);
                option.put("mainType", e.mainType);
                option.put("mainTypeName", e.mainTypeName);
                options.add(option);
            });
            map.put("options", options);
            res.add(map);
        }
        return res;
    }

    public static StorageAreaEnum getAreaEnum(Integer type) {
        for (StorageAreaEnum storageAreaEnum : values()) {
            if (storageAreaEnum.type.equals(type)) {
                return storageAreaEnum;
            }
        }
        return null;
    }

    public static String getAreaTypeName(Integer type) {
        for (StorageAreaEnum storageAreaEnum : values()) {
            if (storageAreaEnum.type.equals(type)) {
                return storageAreaEnum.getTypeName();
            }
        }
        return "未知类型";
    }

    public static List<Map<String, Object>> getMapListInit() {
        List<Map<String, Object>> maps = new ArrayList<>();
        for (StorageAreaEnum value : StorageAreaEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", value.type);
            map.put("typeName", value.typeName);
            map.put("mainType", value.mainType);
            map.put("mainTypeName", value.mainTypeName);
            maps.add(map);
        }
        return maps;
    }

    @Override
    public Integer getValue() {
        return type;
    }

    public String getTypeName() {
        return typeName;
    }

    public Boolean getMainType() {
        return mainType;
    }

    public String getMainTypeName() {
        return mainTypeName;
    }

}
